home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-09-26 | 5.3 KB | 231 lines | [TEXT/KAHL] |
- /* File name : CLaughsApp.cp
- Author : Paul Gee
- Evolution : Bob Krause
- Version : 0.2
- Date : 6th April 1993
- Purpose : Simple NeoAccess Demo.
- Run it the first time and it creates a new database
- and stores some objects.
- Run it a second time and it reads the objects back
- in and prints out their details.
- */
-
- #include "NeoTypes.h"
- #include CNeoMetaClassH
- #include CNeoDatabaseNativeH
- #include CNeoIDListH
- #include CNeoIndexIteratorH
- #include CNeoSelectH
- #include "CLaughsApp.h"
- #include "CPerson.h"
- #include "CNameIndex.h"
- #include <stdlib.h>
- #include <stdio.h>
- #include <time.h>
-
- #define kLaughsCreator 'Neo6'
- #define kLaughsFileType 'Ne6d'
-
- int main(void)
- {
- CLaughsApp * app;
-
- app = new CLaughsApp;
- app->run();
- delete app;
-
- return 0;
- }
-
- /***
- * CLaughsApp
- *
- * Initialize the application. If your application class defines its
- * own instance variables or global variables, this is a good place
- * to initialize them.
- *
- ***/
- CLaughsApp::CLaughsApp(void)
- {
- NeoPerms permissions;
- CNeoMetaClass * meta;
-
- printf("Start Laughing...\n\n");
-
- meta = new CNeoMetaClass(kNameIndexID, kNeoNullClassID, "\pCNameIndex", CNameIndex::New, CNameIndex::KeyManager);
-
- meta = new CNeoMetaClass(kNeoIDListID, kNeoNullClassID, "\pCNeoIDList", CNeoIDList::New, CNeoIDList::KeyManager);
-
- meta = new CNeoMetaClass(kPersonID, kNeoPersistID, "\pCPerson", CPerson::New);
- meta->setKey(kNeoSecondaryIndex, kNameIndexID);
-
- meta = new CNeoMetaClass(kJokerID, kPersonID, "\pCJoker", CJoker::New);
- meta->setKey(kNeoSecondaryIndex, kNameIndexID);
-
- meta = new CNeoMetaClass(kJokeID, kNeoPersistID, "\pCJoke", CJoke::New);
-
- meta = new CNeoMetaClass(kClownID, kPersonID, "\pCClown", CClown::New);
- meta->setKey(kNeoSecondaryIndex, kNameIndexID);
-
- meta = new CNeoMetaClass(kPieID, kNeoPersistID, "\pCPie", CPie::New);
-
- gNeoDatabase = new CNeoDatabaseNative(kLaughsCreator, kLaughsFileType);
- ((CNeoDatabaseNative *)gNeoDatabase)->Specify("\pLaughter", 0);
-
- if (gNeoDatabase->existsOnDisk())
- permissions = NeoReadPerm;
- else {
- gNeoDatabase->create();
- permissions = NeoReadWritePerm;
- }
- gNeoDatabase->open(permissions);
- }
-
- CLaughsApp::~CLaughsApp(void)
- {
- gNeoDatabase->close();
- delete gNeoDatabase;
- gNeoDatabase = nil;
- }
-
- void CLaughsApp::run(void)
- {
- if (gNeoDatabase->getObjectCount(kPersonID, TRUE) > 0)
- printOut();
- else {
- createObjects();
- gNeoDatabase->commit();
- }
- printf("\n\nDone!\n");
- }
-
- /***
- * createObjects
- *
- * Add three people to the database, two jokers and a clown.
- *
- ***/
- void CLaughsApp::createObjects(void)
- {
- short index;
- CJoke * joke1;
- CJoke * joke2;
- CJoker * joker;
- CClown * clown;
- CNeoDatabaseNative *database = (CNeoDatabaseNative *)gNeoDatabase;
- CNeoString name;
-
- database->getName(name);
- name[name.getLength() +1] = 0;
- printf("Store 2 Jokers & a Clown in \"%s\".\n", &name[1]);
-
- // Know any good jokes? How 'bout this one...
- joke1 = new CJoke("The world’s shortest poem: Flees. Adam had'em.");
-
- // Add it to the database.
- // Note: An object ID is assigned automaticly by addObject.
- database->addObject(joke1);
-
- // Is this a joke???
- joke2 = new CJoke("My dogs got no nose?");
-
- // Add it to the database.
- database->addObject(joke2);
-
- // Create a joker object.
- joker = new CJoker("\pJack");
-
- // Teach it a couple of jokes.
- joker->learnJoke(joke1);
- joker->learnJoke(joke2);
-
- // Add it to the database.
- database->addObject(joker);
-
- // Don't need this guy any more. Remove our reference to it.
- joker->unrefer();
- joker = nil;
-
- // Create a clown.
- clown = new CClown("\pFred");
-
- // Add it to the database.
- database->addObject(clown);
-
- // Build up its arsenal.
- clown->bakePie("Jello");
- clown->bakePie("Marshmellow");
- clown->bakePie("Custard");
- clown->bakePie("Cool Whip®");
- clown->bakePie("Yogurt");
-
- // Remember to remove our reference when we're done.
- clown->unrefer();
- clown = nil;
-
- // Create another joker.
- joker = new CJoker("\pHarry");
-
- // Add it to the database.
- database->addObject(joker);
-
- // This guy steals jokes.
- joker->learnJoke(joke2);
-
- // Remove our reference to the joker and the jokes.
- joker->unrefer();
- joker = nil;
-
- for (index = 0; index < 200; index++) {
- joker = new CJoker("\pHarry");
- joker->learnJoke(joke2);
- database->addObject(joker);
- joker->unrefer();
- if ((index&0x7F) == 0)
- database->commit(FALSE);
- }
-
-
- joke1->unrefer();
- joke1 = nil;
- joke2->unrefer();
- joke2 = nil;
- }
-
- /***
- * printOut
- *
- * Find in turn each of the three objects in the database, two jokers and
- * a clown, then commit the changes to disk.
- *
- ***/
- void CLaughsApp::printOut(void)
- {
- CPerson * person;
- CNeoNameSelect key("\p");
- CNeoDatabaseNative *database = (CNeoDatabaseNative *)gNeoDatabase;
- CNeoIndexIterator iterator(database, kPersonID, &key, TRUE);
- CNeoString name;
-
- // Tell them what we're about to do.
- database->getName(name);
- name[name.getLength() +1] = 0;
- printf("Restoring %ld Jokers & %ld Clowns from \"%s\".\n\n",
- database->getObjectCount(kJokerID, FALSE),
- database->getObjectCount(kClownID, FALSE), &name[1]);
-
- // Let's make rand() a little more variable.
- srand((int)time(nil));
-
- // Get the first person object.
- person = (CPerson *)iterator.currentObject();
-
- while(person) {
- person->printName(); // Introductions.
- person->skill(); // Entertain the crowd.
- printf("\n");
- person = (CPerson *)iterator.nextObject(); // Next.
- }
- }
-
-